package com.jzo2o.customer.controller.consumer;

import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 我的地址簿相关接口")
public class AddressBookController {
    @Resource
    private IAddressBookService addressBookService;
    @PostMapping
    @ApiOperation("添加地址")
    public void add(@RequestBody AddressBookUpsertReqDTO reqDTO) {
        addressBookService.add(reqDTO);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询地址")
    public PageResult<AddressBookResDTO> pageQueryAddressBook(AddressBookPageQueryReqDTO reqDTO) {
        return addressBookService.pageQueryAddressBook(reqDTO);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取地址")
    public AddressBookResDTO findById(@PathVariable("id") Long id) {
        return addressBookService.findById(id);
    }

    @PutMapping("/{id}")
    @ApiOperation("修改地址")
    public void update(@PathVariable("id") Long id, @RequestBody AddressBookUpsertReqDTO reqDTO) {
        addressBookService.updateAddressById(id, reqDTO);
    }
    @DeleteMapping("/batch")
    @ApiOperation("删除地址")
    public void delete(@RequestBody List<Long> ids) {
        addressBookService.deleteById(ids);
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public void updateDefault(@RequestParam("id") Long id,@RequestParam("flag") Long flag) {
        addressBookService.updateDefault(id,flag);
    }
    @GetMapping("/defaultAddress")
    @ApiOperation("获取默认地址")
    public AddressBookResDTO getDefaultAddress() {
        return addressBookService.getDefaultAddress();
    }
}
